<template>
  <el-form :model="model"  ref="formRef" v-bind="$attrs">
    <el-row :gutter="10">
      <el-col :span="item.span" v-for="item in schema" :key="item.key">
        <el-form-item :label="item.label" :prop="item.model">
          <!-- input -->
          <template v-if="item.component==='input'">
            <el-input v-bind="item.props||{}" v-model="model[item.model]"/>
          </template>
          
          <!-- select -->
          <template v-else-if="item.component==='select'">
            <el-select v-model="model[item.model]" v-bind="item.props||{}">
              <el-option
                v-for="option in item.selectOptions"
                :key="option.value"
                :label="option.label"
                :value="option.value"
              />
            </el-select>
          </template>
          <!-- button -->
          <template v-else-if="item.component==='button'">
            <el-button v-bind="item.props||{}" v-on="item.listeners">{{ item.btnText }}</el-button>
          </template>
          <template v-else-if="item.component==='submit-button'">
            <el-button type="primary" @click="submit(item.handleSubmit)">{{ item.btnText }}</el-button>
          </template>
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item></el-form-item>
  </el-form>
</template>

<script lang="ts">
export default {
  name: "SchemaForm",
};
</script>

<script lang="ts" setup>
import { ref } from "vue";
defineProps<{
  schema: any;
  model: any;
}>();

const formRef = ref();

const submit = async (cb: () => void) => {
  // 进行表单校验
  await formRef.value.validate();
  // 通过了, 执行外部传入的回调函数
  cb();
};
</script>

<style scoped></style>
